local function push_log(premature, log)
  local http = require "resty.http"  
  local httpc = http.new()  
  local url = "http://ip:host/api/saveaccesslog"  

  local res, err = httpc:request_uri(url, {  
      method = "POST", 
       body = log,  
       headers = {  
           ["Content-Type"] = "application/json",  
       }  
    })  

    if not res then  
         ngx.log(ngx.ERR, "failed to request: ", err)
         return
    end  

end

local cjson = require "cjson"  
local log_json = {}  

local headers=ngx.req.get_headers()
local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr

log_json["uri"]=ngx.escape_uri(ngx.var.request_uri)  
log_json["host"]=ngx.var.host  
log_json["remoteAddr"] = ip  
log_json["timeLocal"] = ngx.var.time_local  
log_json["status"] = ngx.var.status  
log_json["bodyBytesSent"] = ngx.var.body_bytes_sent  
log_json["upstreamAddr"] = ngx.var.upstream_addr  
log_json["upstreamResponseTime"] = ngx.var.upstream_response_time  
log_json["upstreamStatus"] = ngx.var.upstream_status  

-- 转换json为字符串  
local logstr = cjson.encode(log_json)

local ok,err = ngx.timer.at(0, push_log, logstr)

if err then
  ngx.log(ngx.ERR, "failed create timer, ", err)
  return
end